package com.aiti.lulian.service.impl;

import com.aiti.base.core.page.PageInfo;
import com.aiti.base.core.page.PageResult;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardTemplateStyleFieldDto;
import com.aiti.lulian.entity.CardTemplateStyleField;
import com.aiti.lulian.mapper.CardTemplateStyleFieldMapper;
import com.aiti.lulian.service.CardTemplateStyleFieldService;
import com.aiti.lulian.vo.CardTemplateStyleFieldVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CardTemplateStyleFieldServiceImpl extends ServiceImpl<CardTemplateStyleFieldMapper, CardTemplateStyleField> implements CardTemplateStyleFieldService {
    @Override
    public Message saveField(CardTemplateStyleField cardTemplateStyleField) {
        this.save(cardTemplateStyleField);
        return Message.success();
    }

    @Override
    public Message updateField(CardTemplateStyleField cardTemplateStyleField) {
        this.updateById(cardTemplateStyleField);
        return Message.success();
    }

    @Override
    public Message deleteField(String id) {
        this.removeById(id);
        return Message.success();
    }

    @Override
    public Message detail(String id) {
        CardTemplateStyleField entity = this.getById(id);
        return Message.success(entity);
    }

    @Override
    public Message fieldList(Page page, CardTemplateStyleFieldDto dto) {
        LambdaQueryWrapper<CardTemplateStyleField> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(CardTemplateStyleField::getBaseCreateTime);
        Page page1 = this.page(page, wrapper);
        PageResult rspData = new PageResult(page1.getRecords(), new PageInfo(dto.getPageNum(), dto.getPageSize()), page1.getTotal());
        return Message.success(rspData);
    }

    @Override
    public Message deleteFieldByTemplate(String templateId) {
        LambdaQueryWrapper<CardTemplateStyleField> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CardTemplateStyleField::getTemplateId, templateId);
        this.remove(wrapper);
        return Message.success();
    }

    @Override
    public Message fieldListNoRequired(CardTemplateStyleFieldDto dto) {

        List<CardTemplateStyleFieldVo> cardTemplateStyleFieldVos = this.getBaseMapper().fieldListNoRequired(dto);

        Map<String, List<CardTemplateStyleFieldVo>> map = cardTemplateStyleFieldVos.stream()
                .collect(Collectors.groupingBy(
                        CardTemplateStyleFieldVo::getLabelName
                ));
        return Message.success(map);

    }


}